package ${basepackage}.base;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import ${basepackage}.common.PageModel;
import ${basepackage}.util.DaoUtils;
import ${basepackage}.util.StringUtils;

public abstract class BaseDAO extends NamedParameterJdbcDaoSupport {
	
	//if the db change,modify this variable's value
	public static final String DB_TYPE = DaoUtils.DBNAME_MYSQL;
     
	/**
	 * get page list
	 * @param pageNum
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams){
		 
		return getPageList(pageNum, itemCount, sql, sqlParams, null, null, null);
	}
	
	/**
	 * get page list
	 * @param pageNum
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams, RowMapper rowMapper){
		
		return getPageList(pageNum, itemCount, sql, sqlParams, null, null, rowMapper);	
	}	
	
	/**
	 * get page list
	 * @param pageNum
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams, String orderField,String orderType){
		
		return getPageList(pageNum, itemCount, sql, sqlParams, orderField, orderType, null);
	}
	
	/**
	 * get page list
	 * @param pageNum
	 * @param itemCount
	 * @param sql
	 * @param sqlParams
	 * @param orderField
	 * @param orderType
	 * @param rowMapper
	 * @return
	 */
	public PageModel getPageList(int pageNum, int itemCount, String sql, Object[] sqlParams, String orderField,String orderType, RowMapper rowMapper){
		
		//get the total count sql
		String getTotalSql = "SELECT COUNT(0) FROM (" + sql + ") V";
		
		//get the records sql
		if(StringUtils.isNotEmpty(orderField)){
			sql += " ORDER BY " + orderField + " " + orderType;
		}
		
		String getResultSql = DaoUtils.buildPageSql(Integer.toString(pageNum), Integer.toString(itemCount), sql, DB_TYPE);
		
		int totalCount = getJdbcTemplate().queryForInt(getTotalSql, sqlParams);
		
		List resultList = null;
		if(rowMapper == null){
			resultList =  getJdbcTemplate().queryForList(getResultSql, sqlParams);
		}else{
			resultList =  getJdbcTemplate().query(getResultSql, sqlParams, rowMapper);
		}
		
		PageModel pageModel = new PageModel(pageNum, itemCount, totalCount, resultList);
		
        return pageModel;
	
	}	
	
	/**
	 * get vo by id
	 * @param <T>
	 * @param strSql
	 * @param id
	 * @param t
	 * @return
	 */
	public <T> T getVO(String strSql, String id, T t){
		
		List<T> resultList = getJdbcTemplate().query(strSql, new Object[]{id}, new BeanPropertyRowMapper(t.getClass()));
		if(resultList != null && resultList.size() > 0){
			return resultList.get(0);
		}else{
			return null;
		}
	}
	
	public <T> String insertVO(String strSql, T t){
				
		KeyHolder keyHolder = new GeneratedKeyHolder(); 
		int i = getNamedParameterJdbcTemplate().update(strSql, new BeanPropertySqlParameterSource(t), keyHolder);
		if(i > 0){
			return keyHolder.getKey().toString();
		}else{
			return null;
		}
	}
	
	public <T> boolean updateVO(String strSql, T t){
		
		int i = getNamedParameterJdbcTemplate().update(strSql, new BeanPropertySqlParameterSource(t));
		if(i > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean deleteVO(String strSql, String id){
		
		int i = getJdbcTemplate().update(strSql, new Object[]{id});
		if(i > 0){
			return true;
		}else{
			return false;
		}
	}
	
	
	public String getSequenceString(String seq) {	
		
		String strSql = "SELECT " + seq + ".NEXTVAL FROM DUAL";
		int genId = getJdbcTemplate().queryForInt(strSql);
		return Integer.toString(genId);
	}	
	
}
